/**
 * @author harry
 * @date 2021/12/7/21:10
 */

package com.ui.contact;

import com.controller.ContactController;
import com.pojo.Contact;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;

public class AllContact {
    private final ContactController contactController = new ContactController();
    public AllContact(JTable table) {
        DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
        tableModel.setRowCount(0);//clear
        List<Contact> contactList = contactController.findAll();
        if(contactList == null) {
            JOptionPane.showMessageDialog(null, "查询失败！");
            return;
        }
        for (Contact contact : contactList) {
            String[] tableRow = {
                    contact.getName(),
                    contact.getPhone(),
                    contact.getGender(),
                    contact.getAddress(),
                    contact.getEmail(),
                    contact.getGroup(),
                    (contact.isStar()) ? "是" : ""
            };
            tableModel.addRow(tableRow);
        }
        table.setModel(tableModel);
    }
}
